package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class QueryTask
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/QueryTask" })
public class QueryTask extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		

		String userName=request.getParameter("userName");
		String first_time=request.getParameter("first_time");
		String second_time=request.getParameter("second_time");
		java.sql.Date date1=java.sql.Date.valueOf(first_time);
		java.sql.Date date2=java.sql.Date.valueOf(second_time);
		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		/*System.out.print(date1.getClass());
		System.out.print(date2.getClass());*/
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
	//	ResultSetMetaData data =null;
		try {
			connection = JdbcUtils.getConnection();
			String sql = "select task.id,task.maintenanceplan_id,task.submit_time,task.status,task.type,task.name,contract.project_name from task,users,contract where (task.status='待审核' or task.status='审核通过') and (submit_time between ? and ? ) and users.userName=? and users.userId=task.app_id_account and task.maintenance_project=contract.Id ";
			ps = connection.prepareStatement(sql);
			ps.setDate(1, date1);
			ps.setDate(2, date2);
			ps.setString(3, userName);
			
			rs = ps.executeQuery();
			JSONArray task_ja=new JSONArray();
			MYJSONObject task_jb=new MYJSONObject();
			while (rs.next()) {
				task_jb.put("task_id", rs.getString(1));
				task_jb.put("maintenanceplan_id", rs.getString(2));
				task_jb.put("submit_time", rs.getString(3));
				task_jb.put("task_status", rs.getString(4));
				task_jb.put("task_type", rs.getString(5));
				task_jb.put("task_name", rs.getString(6));
				task_jb.put("project_name", rs.getString(7));
				task_ja.add(task_jb.toString());
			}
			
				result_jb.put("status", "true");
				result_jb.put("task", task_ja);
			
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
